package com.poka.entity;

/**
 * Created by Administrator on 2017/12/11.
 */

import android.util.Log;

import java.util.ArrayList;

public class ReciveList {

    private static final String TAG = "ReciveList";
    private ArrayList<byte[]> receivebytes;
    private ArrayList<Boolean> receivefinish;

    public ReciveList() {
        this.receivebytes = new ArrayList();
        this.receivefinish = new ArrayList();
    }

    public void addElement(byte[] buffer, Boolean isfinished) {
        this.receivebytes.add(buffer);
        try {
            Thread.sleep(3L);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.receivefinish.add(isfinished);
        System.out.println("测试" + buffer + isfinished);
    }

    public void modifyLastElement(byte[] buffer, Boolean isfinished) {
        int size = this.receivebytes.size();
        this.receivebytes.remove(size - 1);
        this.receivefinish.remove(size - 1);
        this.receivebytes.add(buffer);
        this.receivefinish.add(isfinished);
    }

    public byte[] getLastElement() {
        int size = this.receivebytes.size();
        byte[] returnbytes = (byte[]) this.receivebytes.get(size - 1);
        return returnbytes;
    }

    public boolean getLastElementStatus() {
        int size = this.receivefinish.size();
        Boolean returnstatus = (Boolean) this.receivefinish.get(size - 1);
        return returnstatus.booleanValue();
    }

    public int getElementNum() {
//            addElement(buffer,isfinished);


        Log.e(TAG, "Receiver!!!!!!" + this.receivebytes.size());
        return this.receivebytes.size();
    }

    /**
     * @param buffer
     * @param isfinished
     * @return
     */
    public int getElementNum(byte[] buffer, Boolean isfinished) {

        addElement(buffer, isfinished);
        return this.receivebytes.size();
    }

    /**
     * @return
     */
    public byte[] getFinishElement() {
        byte[] returnbytes = "".getBytes();
        if (this.receivebytes.size() == 0) {

            Log.e(TAG, "方法进入返回returnbytes" + returnbytes);
            return returnbytes;
        }

        try {
            if (((Boolean) this.receivefinish.get(0)).booleanValue()) {
                returnbytes = new byte[((byte[]) this.receivebytes.get(0)).length];
                System.arraycopy(this.receivebytes.get(0), 0, returnbytes, 0, receivebytes.get(0).length);
                this.receivebytes.remove(0);
                this.receivefinish.remove(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("异常" + e);
        }
        return returnbytes;
    }

    public void removeLastElement() {
        int size = this.receivebytes.size();
        if (size > 0) {
            this.receivebytes.remove(size - 1);
            this.receivefinish.remove(size - 1);
        }
    }

    public void removeAllElement() {
        while (this.receivebytes.size() > 0) {
            this.receivebytes.remove(0);
            this.receivefinish.remove(0);
        }
    }
}

